#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cassert>

#ifdef __CUDACC__
  #include <cuda_runtime.h>
#endif

#define CHECK(call)                                                           \
{                                                                             \
  const cudaError_t error=call;                                               \
  if (error != cudaSuccess)                                                   \
  {                                                                           \
    printf ("ERROR: %s:%d, ", __FILE__, __LINE__);                            \
    printf ("code: %d, reason: %s\n", error, cudaGetErrorString (error));     \
    exit (1);                                                                 \
  }                                                                           \
}

using namespace std;

#define PRODUCTION_MAX 512
#define ITEM_MAX 128
#define VN_MAX 128
#define VT_MAX 128
#define STRING_MAX 1024

/**
 * used to index the sorted productions and items
 * @ptr:    means the subscript of production[] or item[]
 * @length: means the same type size
**/
struct Index
{
  int ptr;
  int size;
  Index (): ptr(-1), size(0) { }
};

/**
 * record production like <0>::=<1><2>
 * @A:  <0>
 * @B:  <1>
 * @C:  <2>
**/
struct Production
{
  int A;
  int B;
  int C;
};

/**
 * record production like <0>::=<1>
 * @A:  <0>
 * @B:  <1>
**/
struct Item
{
  int A;
  char B;
};

// index of Item by item.B
Index i_idx[VT_MAX];

Production production[PRODUCTION_MAX];

Item item[ITEM_MAX];

/**
 * h_dp[i][j][v] = n
 * There are n time of situation for string[i, j] to reduce to v
**/
// unsigned int h_dp[STRING_MAX][STRING_MAX][VN_MAX];
unsigned int h_dp[STRING_MAX * STRING_MAX * VN_MAX];

size_t size = STRING_MAX * STRING_MAX * VN_MAX;

int vn_num;
int production_num;
int item_num;
int string_length;
char str[STRING_MAX];

__host__ __device__ inline int index(const int x, const int y, const int z) {
  return x * STRING_MAX * VN_MAX + y * VN_MAX + z;
}

__global__ void node(
  unsigned int *d_dp,
  Production *d_production,
  int production_num,
  int len)
{
  int l = blockIdx.x * blockDim.x + threadIdx.x, r = l + len - 1;
  for (int mid = l + 1; mid <= r; mid++)
  {
    for (int p = 0; p < production_num; p++)
    {
      d_dp[index(l, r, d_production[p].A)]
        += d_dp[index(l, mid - 1, d_production[p].B)]
        * d_dp[index(mid, r, d_production[p].C)];
    }
  }
}

int main(int argc, char **argv)
{
  // basic io
  if (argc != 2)
  { printf ("cyk <input.txt>"); return 0; }

  freopen(argv[1], "r", stdin);
  scanf("%d\n", &vn_num); scanf("%d\n", &production_num);
  for (int i = 0; i < production_num; i++)
    scanf("<%d>::=<%d><%d>\n", &production[i].A, &production[i].B, &production[i].C);
  scanf("%d\n", &item_num);
  for (int i = 0; i < item_num; i++)
    scanf("<%d>::=%c\n", &item[i].A, &item[i].B);
  scanf("%d\n", &string_length); scanf("%s\n", str);

  // sort ascending order by item.B and item.A
  sort (item, item + item_num, [](const Item& a,
                                  const Item& b)
  { return a.B == b.B ? a.A < b.A : a.B < b.B; });

  // init the index which indexed by [item.B]
  for (int i = 0; i < item_num; i++)
  {
    int t = item[i].B;
    if (i_idx[t].ptr == -1) i_idx[t].ptr = i;
    i_idx[t].size++;
  }

  // compute len is 1 situation
  for (int i = 0; i < string_length; i++)
  {
    int t = str[i];
    for (int j = i_idx[t].ptr; j < i_idx[t].ptr + i_idx[t].size; j++)
      h_dp[index(i, i, item[j].A)] = 1;
  }

  // for cuda
  unsigned int *d_dp, res;
  Production *d_production;

  CHECK (cudaMalloc (&d_dp, sizeof (h_dp)));
  CHECK (cudaMemcpy (d_dp, h_dp, sizeof (h_dp), cudaMemcpyHostToDevice));
  CHECK (cudaMalloc (&d_production, sizeof (production)));
  CHECK (cudaMemcpy (d_production, production, sizeof (production), cudaMemcpyHostToDevice));

  // compute len in [2, string_length] situation
  for (int len = 2; len <= string_length; len++)
  {
    node<<<1, string_length - len + 1>>> (d_dp, d_production, production_num, len);
  }

  CHECK (cudaMemcpy (&res, &d_dp[index(0, string_length - 1, 0)], sizeof (int), cudaMemcpyDeviceToHost));
  printf ("%u\n", res);

  CHECK (cudaFree (d_dp));
  CHECK (cudaFree (d_production));

  cudaDeviceReset();
  return 0;
}
